package com.lsk.netdisk.auth.interceptor;

import com.lsk.netdisk.auth.util.SpringUtil;
import com.lsk.netdisk.common.redis.core.RedisComponent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class GatewayInterceptor extends HandlerInterceptorAdapter {

	private RedisComponent redisComponent = SpringUtil.getBean(RedisComponent.class);

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		String reqToken = request.getParameter("reqToken");
		if((reqToken == null || reqToken.equals("")) && !isFormGateway(request)){
			return false;
		}
		String reqID = reqToken.split(":")[0];
		String tokenValue = reqToken.split(":")[1];
		String redisToken = redisComponent.rawGet(reqID);
		if((!tokenValue.equals(redisToken) && !isFormGateway(request))){
			return false;
		}
		return true;
	}

	private boolean isFormGateway(HttpServletRequest req){
		return req.getHeader("Host").equals("192.168.0.6:30000");
	}
}
